package com.sugon.api;

import com.sugon.annotation.AuthCheck;
import com.sugon.annotation.LoginUser;
import com.sugon.entity.*;
import com.sugon.modules.device.consts.DevMaintenanceTicketStatus;
import com.sugon.modules.tools.service.IToolsCheckTicketManager;
import com.sugon.modules.utils.DateUtil;
import com.sugon.service.ToolsCheckJudgeStandardService;
import com.sugon.service.ToolsCheckTicketItemService;
import com.sugon.service.ToolsCheckTicketService;
import com.sugon.util.ApiBaseAction;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * 工装周期性检测工单API
 *
 * @author YuXD
 * @since 2022-05-27
 */
@Api(tags = "工装周期性检测工单API")
@RestController
@RequestMapping("/api/v1/toolscheckticket")
public class ToolsCheckTicketApiController extends ApiBaseAction {

    @Autowired
    private ToolsCheckTicketService toolsCheckTicketService;
    @Autowired
    private IToolsCheckTicketManager toolsCheckTicketManager;
    @Autowired
    private ToolsCheckTicketItemService toolsCheckTicketItemService;
    @Autowired
    private ToolsCheckJudgeStandardService toolsCheckJudgeStandardService;

    /**
     * 获取工装周期性检测工单API
     * 只返回当月未完成的工单
     *
     * @param loginUser 当前登录用户
     * @param params    查询参数
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "获取工装周期性检测工单API，只返回当月未完成的工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "devName", value = "设备名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "deptCode", value = "设备所属组织ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "limit", value = "每页记录数", paramType = "query", dataType = "int", required = true)
    })
    @PostMapping
    public ApiResult<PageUtils> getRelatedSparePartList(@ApiIgnore @LoginUser SysUserEntity loginUser, @ApiIgnore @RequestParam Map<String, Object> params) {
        ApiResult<PageUtils> apiResult = new ApiResult<>();
        // 只返回维护中且当月的检测工单
        params.put("state", DevMaintenanceTicketStatus.MAINTAINING);
        params.put("checkMonth", DateUtil.getYearAndMonth(null));
        //查询列表数据
        Query query = new Query(params);
        List<ToolsCheckTicketEntity> toolsCheckTicketList = toolsCheckTicketService.queryList(query);
        int total = toolsCheckTicketService.queryTotal(query);
        return apiResult.success(new PageUtils(toolsCheckTicketList, total, query.getLimit(), query.getPage()));
    }

    /**
     * 获取工单设备关联的工装列表API
     *
     * @param loginUser 当前登录用户已
     * @param ticketId  工单ID
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "获取工单设备关联的工装列表API")
    @ApiImplicitParam(name = "ticketId", value = "检测工单ID", paramType = "path", dataType = "String")
    @PostMapping("/{ticketId}/toolsBase")
    public ApiResult<List<ToolsBaseEntity>> getTicketToolsBaseList(@ApiIgnore @LoginUser SysUserEntity loginUser, @PathVariable("ticketId") String ticketId) {
        ApiResult<List<ToolsBaseEntity>> apiResult = new ApiResult<>();
        return apiResult.success(toolsCheckTicketManager.getTicketToolsBaseList(ticketId));
    }

    /**
     * 获取工装检测标准API
     *
     * @param loginUser 当前登录用户
     * @param params    查询参数
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "获取工装检测标准API")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "toolsFileId", value = "工装ID", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "limit", value = "每页记录数", paramType = "query", dataType = "Integer", required = true)
    })
    @PostMapping("/toolsCheckStandard")
    public ApiResult<PageUtils> loadToolsCheckStandard(@ApiIgnore @LoginUser SysUserEntity loginUser, @ApiIgnore @RequestParam Map<String, Object> params) {
        ApiResult<PageUtils> apiResult = new ApiResult<>();
        //查询列表数据
        Query query = new Query(params);
        List<ToolsCheckJudgeStandardEntity> toolsCheckJudgeStandardList = toolsCheckJudgeStandardService.queryList(query);
        int total = toolsCheckJudgeStandardService.queryTotal(query);
        return apiResult.success(new PageUtils(toolsCheckJudgeStandardList, total, query.getLimit(), query.getPage()));
    }

    /**
     * 获取工装周期性检测工单明细API
     *
     * @param loginUser 当前登录用户
     * @param params    查询参数
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "获取工装周期性检测工单明细API")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticketId", value = "工装周期性检测工单ID", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "limit", value = "每页记录数", paramType = "query", dataType = "Integer", required = true)
    })
    @PostMapping("/items")
    public ApiResult<PageUtils> getTicketItems(@ApiIgnore @LoginUser SysUserEntity loginUser, @ApiIgnore @RequestParam Map<String, Object> params) {
        ApiResult<PageUtils> apiResult = new ApiResult<>();
        Query query = new Query(params);
        List<ToolsCheckTicketItemEntity> toolsCheckTicketItemList = toolsCheckTicketItemService.queryList(query);
        int total = toolsCheckTicketItemService.queryTotal(query);
        return apiResult.success(new PageUtils(toolsCheckTicketItemList, total, query.getLimit(), query.getPage()));
    }

    /**
     * 更新测量值或更换时间API
     *
     * @param loginUser            当前登录用户
     * @param toolsCheckTicketItem 周期性检测工单明细信息
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "更新测量值或更换时间API", notes = "测量值和更换时间必须二选一")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工单明细ID", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "measuredValue", value = "测量值", paramType = "query", dataType = "double"),
            @ApiImplicitParam(name = "changeDate", value = "更换时间，格式：yyyy-MM-dd", paramType = "query", dataType = "String")
    })
    @PostMapping("/updateMeasuredValue")
    public ApiResult<String> updateMeasuredValue(@ApiIgnore @LoginUser SysUserEntity loginUser, @ApiIgnore ToolsCheckTicketItemEntity toolsCheckTicketItem) {
        ApiResult<String> apiResult = new ApiResult<>();
        if (toolsCheckTicketItem.getMeasuredValue() == null && toolsCheckTicketItem.getChangeDate() == null) {
            return apiResult.failMsg("测量值或更换时间必须二者有一个");
        }
        return toolsCheckTicketManager.updateMeasuredValue(toolsCheckTicketItem) ? apiResult.success() : apiResult.failMsg("更新失败！");
    }

}
